package com.example.friend.rabbitmq;


import com.example.core.constants.RabbitMQConstants;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMqInit  {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    //创建交换机和对列
    @PostConstruct
    public void init() {
        log.info("***************** init rabbitMQ ****************");
        DirectExchange directExchange =
                new DirectExchange(RabbitMQConstants.DIRECT_EXCHANGE_NAME, true, false);
        Queue queue =
                new Queue(RabbitMQConstants.DIRECT_JUDGE_QUEUE_NAME, true);
        rabbitAdmin.declareExchange(directExchange);
        rabbitAdmin.declareQueue(queue);
        rabbitAdmin.declareBinding(new Binding(RabbitMQConstants.DIRECT_JUDGE_QUEUE_NAME,
                        Binding.DestinationType.QUEUE, RabbitMQConstants.DIRECT_EXCHANGE_NAME,
                        RabbitMQConstants.DIRECT_JUDGE_ROUTE_KEY, null));
        log.info("***************** rabbitMQ init success ****************");
    }
}
